﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_PartReceive : UserControl
    {
        public panel_PartReceive()
        {
            InitializeComponent();
            LoadData();

            PartReceive.PopupMenuShowing += PartReceive_PopupMenuShowing;
            partReveiceContext.ItemClicked += partReveiceContext_ItemClicked;
        }

        private void callEditForm()
        {
            MessageBox.Show("Cannot Edit now.");
        }

        void partReveiceContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.partReveiceContext.Hide();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;                
                case "addSelection":
                    frm_Add_PartReceive add_Form = new frm_Add_PartReceive();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void PartReceive_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.partReveiceContext.Show(MousePosition);            
        }
        

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT t1.ID, t1.POID, t1.OrderNo, t2.PartNo, t3.IQID, t1.Quantity, t1.ArrivalDate, t1.Note,"
                + " t1.Finished, t3.BuyingUnit, t4.UnitID AS IQUnit, t3.[Buying/IQEx],"
                + " t2.Quantity"
                + " FROM [241_PartReceive] AS t1 LEFT JOIN"                
                + " [241_PODetails] AS t2 ON t1.POID=t2.POID AND t1.OrderNo=t2.OrderNo COLLATE Vietnamese_CI_AS LEFT JOIN"
                + " [222_Part] AS t3 ON t2.PartNo=t3.PartNo COLLATE Vietnamese_CI_AS LEFT JOIN"
                + " [222_IQ] AS t4 ON t3.IQID = t4.IQID"
                + " WHERE (DATEDIFF(day,t1.ArrivalDate,GETDATE()) < 365)";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                foreach (DataRow part_receive in sourceDataSet.Tables[0].Rows)
                {
                    if (!string.IsNullOrEmpty(part_receive["BuyingUnit"].ToString().Trim()))
                    {
                        if (((int)(part_receive["BuyingUnit"].ToString().Trim()[0])) == 65402)
                        {
                            part_receive.BeginEdit();
                            part_receive["BuyingUnit"] = "PC";
                            part_receive.EndEdit();
                        }
                    }
                }
                gridPartReceive.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("POID", typeof(string));
                newTable.Columns.Add("OrderID", typeof(string));
                newTable.Columns.Add("PartID", typeof(string));
                newTable.Columns.Add("IQID", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("ArrivalDate", typeof(DateTime));
                newTable.Columns.Add("Note", typeof(string));
                newTable.Columns.Add("Finished", typeof(int));                
                newTable.Columns.Add("BuyingUnit", typeof(string));
                newTable.Columns.Add("IQUnit", typeof(string));
                newTable.Columns.Add("Buying/IQEx", typeof(float));
                newTable.Columns.Add("CusQuantity", typeof(float));
                gridPartReceive.DataSource = newTable;
            }
            PartReceive.Columns[1].Caption = "POID";
            PartReceive.Columns[2].Caption = "Order No";
            PartReceive.Columns[3].Caption = "Part No";
            PartReceive.Columns[4].Caption = "IQID";
            PartReceive.Columns[5].Caption = "Quantity";
            PartReceive.Columns[6].Caption = "Arrival Date";
            PartReceive.Columns[7].Caption = "Note";
            PartReceive.Columns[8].Caption = "Finished";            
            PartReceive.Columns[9].Caption = "Buying Unit";
            PartReceive.Columns[10].Caption = "IQUnit";
            PartReceive.Columns[11].Caption = "Buying/IQ Exchange";
            PartReceive.Columns[12].Caption = "Custom Quantity";
            PartReceive.Columns[0].Visible = false;
            PartReceive.BestFitColumns();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_PartReceive add_Form = new frm_Add_PartReceive();
            add_Form.ShowDialog();
            LoadData();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            // Call Edit Form
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            frm_Import_PartReceive import_form = new frm_Import_PartReceive();
            import_form.ShowDialog();
        }
    }
}
